package com.lpd100.china.chenchen.hj;

/**
 * 写出一个程序，接受一个由字母、数字和空格组成的字符串，和一个字符，然后输出输入字符串中该字符的出现次数。（不区分大小写字母）
 * <p> <功能详细描述> </p>
 *
 * @author 陈晨
 * @version 1.0
 * @date 2023/8/25
 */
public class HJ2 {
    public static final HJ2 INSTANCE = new HJ2();

    public int action(String input, String ch) {
        System.out.println("input: [" + input + "], ch: [" + ch + "]");
        if (!input.matches("[0-9a-zA-Z ]+")) {
            throw new RuntimeException("仅接受由字母、数字和空格组成的字符串");
        }
        String[] words = input.split(ch);
        return words.length - 1;
    }

    public static void main(String[] args) {
        String input = "hello world";
        String ch = "w";
        System.out.println(HJ2.INSTANCE.action(input, ch));
        input = "hello world    ";
        System.out.println(HJ2.INSTANCE.action(input, ch));
        input = "hello world hello world hello worldhello worldhello worldhello worldhello worldhello worldhello world";
        System.out.println(HJ2.INSTANCE.action(input, ch));
        input = "hello";
        System.out.println(HJ2.INSTANCE.action(input, ch));
    }

}


